<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-date-picker
        v-model="listQuery.resYear"
        class="filter-item"
        style="width: 160px"
        type="year"
        value-format="yyyy"
        placeholder="获得年度"
      />
      <el-input
        v-model="listQuery.resName"
        clearable
        class="filter-item"
        style="width: 160px"
        placeholder="专利名称"
      />
      <el-input
        v-model="listQuery.resMark"
        clearable
        class="filter-item"
        style="width: 160px"
        placeholder="专利号"
      />
      <el-input
        v-model="listQuery.resWithProjectName"
        clearable
        class="filter-item"
        style="width: 160px"
        placeholder="资助项目"
      />
      <el-input
        v-model="listQuery.dutyPersonalName"
        clearable
        class="filter-item"
        style="width: 160px"
        placeholder="第一完成人"
      />
      <el-select
        v-model="listQuery.statusAudit"
        clearable
        class="filter-item"
        style="width: 160px"
        placeholder="审核状态"
      >
        <el-option
          v-for="item in statusAuditArr"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >查找</el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >添加</el-button>
      <!-- <el-button
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="handleDownload"
        >导出</el-button
      > -->
    </div>
    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="table-expand">
            <el-form-item label="获得年度">
              <span>{{ props.row.resYear }}</span>
            </el-form-item>
            <el-form-item label="专利名称">
              <span>{{ props.row.resName }}</span>
            </el-form-item>
            <el-form-item label="颁发机构">
              <span>{{ props.row.resAuthAgency }}</span>
            </el-form-item>
            <el-form-item label="专利类别">
              <span>{{ props.row.resType }}</span>
            </el-form-item>
            <el-form-item label="专利号">
              <span>{{ props.row.resMark }}</span>
            </el-form-item>
            <!-- <el-form-item label="授权公告号">
              <span>{{ props.row.res_reserve1 }}</span>
            </el-form-item> -->
            <el-form-item label="级别">
              <span>{{ levelMap[props.row.resLevel] }}</span>
            </el-form-item>
            <!-- <el-form-item label="影响因子">
              <span>{{ props.row.resLevelInfo }}</span>
            </el-form-item> -->
            <el-form-item label="第一完成人">
              <span>{{ props.row.dutyPersonalName }}</span>
            </el-form-item>
            <el-form-item label="资助项目">
              <div v-if="props.row.resWithProjectName">
                <span
                  v-for="(item, index) in props.row.resWithProjectName.split(
                    ','
                  )"
                  :key="index"
                  style="margin-right: 5px"
                >
                  <el-tag v-if="item != ''" size="mini" type="info">{{
                    item
                  }}</el-tag>
                </span>
              </div>
            </el-form-item>
            <!-- <el-form-item label="专利申请日">
              <span>{{ props.row.resFinishDate }}</span>
            </el-form-item> -->
            <!-- <el-form-item label="授权公告日">
              <span>{{ props.row.resPassDate }}</span>
            </el-form-item> -->
            <el-form-item label="附件">
              <span v-if="props.row.resFilesName.toString() != '[]'">
                <el-button
                  v-for="(item, index) in JSON.parse(props.row.resFilesName)"
                  :key="index"
                  type="success"
                  plain
                  size="mini"
                  @click="downloadFile(item, props.row.resId)"
                >{{ item }}</el-button>
              </span>
            </el-form-item>
            <el-form-item label="备注">
              <span>{{ props.row.resRemark }}</span>
            </el-form-item>
            <el-form-item label="审核状态">
              <span>{{ statusAuditMap[props.row.statusAudit] }}</span>
            </el-form-item>
            <el-form-item label="退回理由">
              <span>{{ props.row.backupRemark }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column align="center" label="获得年度" prop="resYear" />
      <el-table-column align="center" label="专利名称" prop="resName" />
      <el-table-column align="center" label="颁发机构" prop="resAuthAgency" />
      <el-table-column align="center" label="专利类别" prop="resType" />
      <el-table-column align="center" label="专利号" prop="resMark" />
      <!-- <el-table-column align="center" label="授权公告号" prop="res_reserve1" /> -->
      <el-table-column align="center" label="级别" prop="resLevel">
        <template slot-scope="scope">
          <span>{{ levelMap[scope.row.resLevel] }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="影响因子" prop="resLevelInfo" /> -->
      <el-table-column
        align="center"
        label="第一完成人"
        prop="dutyPersonalName"
      />
      <el-table-column
        align="center"
        label="资助项目"
        prop="resWithProjectName"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.resWithProjectName">
            <span
              v-for="(item, index) in scope.row.resWithProjectName.split(',')"
              :key="index"
            >
              <el-tag v-if="item != ''" size="mini" type="info">{{
                item
              }}</el-tag>
            </span>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="专利申请日" prop="resFinishDate" /> -->
      <!-- <el-table-column align="center" label="授权公告日" prop="resPassDate" /> -->
      <!-- <el-table-column align="center" label="附件" prop="projectFilesName">
        <template slot-scope="scope">
          <span v-if="scope.row.projectFilesName.toString() != '[]'">{{
            JSON.parse(scope.row.projectFilesName).join(',')
          }}</span>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="备注" prop="resRemark" />
      <el-table-column align="center" label="审核状态" prop="statusAudit">
        <template slot-scope="scope">
          <span
            class="status-audit"
            :style="statusAuditStyleMap[scope.row.statusAudit]"
          >{{ statusAuditMap[scope.row.statusAudit] }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="退回理由" prop="backupRemark" /> -->
      <el-table-column
        align="center"
        label="审核操作"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            :disabled="
              !(scope.row.statusAudit == '1' || scope.row.statusAudit == '4')
            "
            type="primary"
            size="mini"
            @click="setSubmit(scope.row)"
          >提交</el-button>
          <el-button
            :disabled="!(scope.row.statusAudit == '2' && isSuperAdmin)"
            type="primary"
            size="mini"
            @click="setPass(scope.row)"
          >通过</el-button>
          <el-button
            :disabled="!(scope.row.statusAudit == '2' && isSuperAdmin)"
            type="primary"
            size="mini"
            @click="setBackup(scope.row)"
          >退回</el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
          <el-button
            :disabled="
              !(
                isSuperAdmin ||
                scope.row.statusAudit == '1' ||
                scope.row.statusAudit == '4'
              )
            "
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

    <el-dialog
      title="退回理由"
      :visible.sync="dialogVisible"
      width="30%"
      @close="cancelBackup"
    >
      <div>
        <el-input
          v-model="backupParams.remark"
          type="textarea"
          :rows="6"
          placeholder="请输入退回理由"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBackup">取消</el-button>
        <el-button type="primary" @click="confirmBackup">确定退回</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDownloadFile,
  auditSubmit,
  auditPass,
  auditBackup
} from '@/api/technology-project/project-manage'
import { resultList, deleteResult } from '@/api/technology-result/result'
import {
  statusAuditArr,
  levelMap,
  statusAuditMap,
  statusAuditStyleMap
} from '@/utils/commonData'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { mapGetters } from 'vuex'
const nowModelId = 4
export default {
  name: 'GoodsList',
  components: { BackToTop, Pagination },
  data() {
    return {
      statusAuditArr,
      levelMap,
      statusAuditMap,
      statusAuditStyleMap,
      list: [],
      total: 0,
      listLoading: true,
      dialogVisible: false,
      backupParams: {
        projectId: '',
        modelId: nowModelId,
        remark: ''
      },
      listQuery: {
        page: 1,
        limit: 20,
        owningModule: nowModelId,
        resYear: '',
        resName: '',
        resMark: '',
        resWithProjectName: '',
        dutyPersonalName: '',
        statusAudit: '',
        sort: 'add_time',
        order: 'desc'
      },
      goodsDetail: '',
      detailDialogVisible: false,
      downloadLoading: false
    }
  },
  created() {
    if (this.$route.query.projectName) {
      this.listQuery.resName = this.$route.query.projectName
    }
    this.getList()
  },
  computed: {
    ...mapGetters(['roles']),
    isSuperAdmin() {
      return this.roles.indexOf('超级管理员') !== -1
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      resultList(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    setSubmit(row) {
      const params = {
        projectId: row.resId,
        modelId: nowModelId
      }
      auditSubmit(params)
        .then((response) => {
          this.$notify.success({
            title: '成功',
            message: '提交成功'
          })
          this.getList()
        })
        .catch(() => {
          this.$notify.error({
            title: '失败',
            message: '提交失败'
          })
        })
    },
    setPass(row) {
      const params = {
        projectId: row.resId,
        modelId: nowModelId
      }
      auditPass(params)
        .then((response) => {
          this.$notify.success({
            title: '通过',
            message: '审核通过'
          })
          this.getList()
        })
        .catch(() => {
          this.$notify.error({
            title: '失败',
            message: '审核失败'
          })
        })
    },
    cancelBackup() {
      this.dialogVisible = false
      this.backupParams = {
        projectId: '',
        modelId: nowModelId,
        remark: ''
      }
    },
    setBackup(row) {
      this.dialogVisible = true
      this.backupParams.projectId = row.resId
    },
    confirmBackup() {
      auditBackup(this.backupParams)
        .then((response) => {
          this.$notify.success({
            title: '成功',
            message: '退回成功'
          })
          this.cancelBackup()
          this.getList()
        })
        .catch(() => {
          this.$notify.error({
            title: '失败',
            message: '退回失败'
          })
          this.cancelBackup()
        })
    },
    downloadFile(fileName, projectId) {
      const params = {
        fileName: fileName,
        projectId: projectId
      }
      listDownloadFile(params)
        .then((response) => {
          if (!response) return
          const blob = new Blob([response.data], {
            type: 'application/vnd.ms-excel;charset=utf-8'
          })
          const url = window.URL.createObjectURL(blob)
          const aLink = document.createElement('a')
          aLink.style.display = 'none'
          aLink.href = url
          aLink.setAttribute('download', fileName)
          document.body.appendChild(aLink)
          aLink.click()
          document.body.removeChild(aLink)
          window.URL.revokeObjectURL(url)
        })
        .catch((response) => {
          if (!response) return
          const blob = new Blob([response.data], {
            type: 'application/vnd.ms-excel;charset=utf-8'
          })
          const url = window.URL.createObjectURL(blob)
          const aLink = document.createElement('a')
          aLink.style.display = 'none'
          aLink.href = url
          aLink.setAttribute('download', fileName)
          document.body.appendChild(aLink)
          aLink.click()
          document.body.removeChild(aLink)
          window.URL.revokeObjectURL(url)
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/technology-result/major-create' })
    },
    handleUpdate(row) {
      this.$router.push({
        path: '/technology-result/major-edit',
        query: { resId: row.resId }
      })
    },
    showDetail(detail) {
      this.goodsDetail = detail
      this.detailDialogVisible = true
    },
        handleDelete(row) {
            this.$confirm('此操作将删除该项目, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                .then(() => {
                    this.handleDeleteProject(row)
                })
                .catch(() => {
                    this.$notify.info({
                        title: '失败',
                        message: response.data.errmsg
                    })
                })
        },
    handleDeleteProject(row) {
      const params = {
        resId: row.resId
      }
      deleteResult(params)
        .then((response) => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          this.getList()
        })
        .catch((response) => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then((excel) => {
        const tHeader = [
          '商品ID',
          '商品编号',
          '专利名称',
          '专柜价格',
          '当前价格',
          '是否新品',
          '是否热品',
          '是否在售',
          '首页主图',
          '宣传图片列表',
          '商品介绍',
          '详细介绍',
          '商品图片',
          '商品单位',
          '关键字',
          '类目ID',
          '品牌商ID'
        ]
        const filterVal = [
          'id',
          'goodsSn',
          'name',
          'counterPrice',
          'retailPrice',
          'isNew',
          'isHot',
          'isOnSale',
          'listPicUrl',
          'gallery',
          'brief',
          'detail',
          'picUrl',
          'goodsUnit',
          'keywords',
          'categoryId',
          'brandId'
        ]
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '商品信息')
        this.downloadLoading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  .el-table {
    .status-audit {
      position: relative;
      font-weight: bold;
      &::before {
        content: '•';
        font-size: 24px;
        color: inherit;
        position: absolute;
        top: -4px;
        left: -14px;
      }
    }
  }
}
</style>
<style>
.table-expand {
  font-size: 0;
}

.table-expand label {
  width: 100px;
  color: #99a9bf;
}

.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}

.gallery {
  width: 80px;
  margin-right: 10px;
}

.goods-detail-box img {
  width: 100%;
}
</style>
